/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package factory2;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import javax.swing.Timer;

/**
 *
 * @author wida46984148
 */
public class PGame extends BasicGameState {

    Timer timer;
    public static final int ID = 14;
    Image imatgeFons;
    StateBasedGame state;
    aux2 a = new aux2();
    float x = 350; // en original 800x600
    //float x = 150;
    float y = 800;
    float zxc = -1300;
    int nivel;
    float enemigolvl;
    //boolean gamestart = false;
    boolean gameend = false;
    boolean gamepause = true;
    NauBase nbW = null;
    NauBase[] AnbW = new NauBase[3];
    NauBase nbE = null;
    NauBase nbBoss = null;
    Shoot msE = null;
    Shoot ms2 = null;
    int id = 0;
    int cnt;
    int disparosr = 0;      //disparos realizados contador pol sabra
    boolean enemigo = true;
    boolean pause = false;
    final String[] oleadaEnemigos = {"nave0", "nave0A", "nave0B", "nave0C", "nave2A", "nave2B", "descanso", "descanso", "descanso", "nave0E", "nave0F", "nave0C", "nave0C", "nave0C"};
    //final String[] oleadaEnemigos = {"nave0"};
    int enemigoF = 0;
    //nave0(nave) nave1(tank) nave2(heli)

    public void init(GameContainer container, StateBasedGame game) throws SlickException {
        this.state = game;
        imatgeFons = new Image("src/data/superLand.png");
        nivel = 1;
        switch (nivel) {
            case 1:
                nbW = Naves.nausWida("nave2");
                a.posicionaUnitatAmiga(x, y, nbW);
                enemigolvl = 0.4f;
                break;
            case 2:
                enemigolvl = 0.5f;
                break;
            case 3:
                nbW = Naves.nausWida("nave3");
                a.posicionaUnitatAmiga(x, y, nbW);
                enemigolvl = 0.6f;
                break;
        }

    }

    public void update(GameContainer gc, StateBasedGame game, int delta) throws SlickException {
        Input input = gc.getInput();

        if (y > 480) {
            y -= 5;
            nbW.setLocation(x, y);

        }
        
        if (!(nbW.destroy)) {
            if (gameend) {
                if (y > -150) {
                    y -= 5;
                    nbW.setLocation(x, y);
                } else {
                    game.enterState(9);
                }
            } else {
                if (y > 480) {
                    y -= 5;
                    nbW.setLocation(x, y);
                }
                if (input.isKeyDown(Input.KEY_A)) {
                    float auxX = x;
                    x += -enemigolvl * delta;
                    if (x <= -30) {
                        x = auxX;
                    }
                    nbW.setLocation(x, y);
                }

                if (input.isKeyDown(Input.KEY_D)) {
                    float auxX = x;
                    x += enemigolvl * delta;
                    if (700 <= x) { //en original 800X600
                        //                if (300 <= x) {
                        x = auxX;
                    }
                    nbW.setLocation(x, y);
                }
                if (input.isKeyDown(Input.KEY_S)) {
                    float hip = enemigolvl * delta;
                    float auxY = y;
                    x += hip * java.lang.Math.sin(java.lang.Math.toRadians(0.0));
                    y += hip * java.lang.Math.cos(java.lang.Math.toRadians(0.0));
                    if (480 <= y) {
                        y = auxY;
                    }
                    nbW.setLocation(x, y);
                }

                if (input.isKeyDown(Input.KEY_W)) {
                    float hip = enemigolvl * delta;
                    float auxY = y;
                    x += hip * java.lang.Math.sin(java.lang.Math.toRadians(0.0));
                    y -= hip * java.lang.Math.cos(java.lang.Math.toRadians(0.0));
                    if (y <= 0) {
                        y = auxY;
                    }
                    nbW.setLocation(x, y);
                }

                if ((int) java.lang.Math.sin(java.lang.Math.toRadians(cnt)) == 1 || (int) java.lang.Math.sin(java.lang.Math.toRadians(cnt)) == -1) {
                    oleadaEnemigos(1);
                }
                cnt += 2;
                if (input.isKeyPressed(Input.KEY_SPACE)) {
                    disparosr++;
                    Shoot ms = null;
                    if (nivel == 1) {
                        ms = nbW.shoot.copiarShoot();
                    } else if (nivel == 3) {
                        ms = nbW.shoot.copiarShoot();
                    }

                    a.posShoot(x + 58, y, ms);
                }

            }
        } else {
            nbW.setLocation(1000, 1000);
            game.enterState(5);
        }

        a.update(delta);
        //zxc += 0.2;
        zxc = zxc != 0 ? zxc += 0.3 : 0;
    }

    public void oleadaEnemigos(int x) {
        if (enemigoF < oleadaEnemigos.length) {
            a.posicionaUnitatEnemiga(oleadaEnemigos[enemigoF]);
            enemigoF += x;
        } else if (enemigoF == oleadaEnemigos.length) {
            a.posicionaUnitatEnemiga("boss1");//boss
            enemigoF += x;
        } else {
            gameend = a.anbE.get(a.anbE.size() - 1).isMort();
        }
    }

    public void render(GameContainer gc, StateBasedGame game, Graphics g)
            throws SlickException {
        imatgeFons.draw(200, zxc);
        a.dibuixar(g, gc);
//        p.dibuixar(g, gc);
//        mi.render(gc, g);


    }

//    @Override
//    public void enter(GameContainer gc, StateBasedGame state) {
//        p = new Tauler(6, 10, 1024, 600);
//        md = new ManagerDinersAures();
//        mi = new MenuIngame(gc, 0, 600, ManagerRecursos.getImage("contenidorIngameImage"), ManagerPerfil.getUnitatsTriades(), md, state);
//        mi.clear();
//        mi.reiniciarBotons();
//        ManagerContext.setDiners(md);
//        ManagerEnemics.iniciarCompteEnrere();
//    }
    @Override
    public int getID() {
        return ID;

    }
}
